//
//  PullCollectionView.m
//  Pods
//
//  Created by apple on 20-6-7.
//
//

#import "PullCollectionView.h"

@interface PullCollectionView (Private) <UIScrollViewDelegate>
- (void) config;
- (void) configDisplayProperties;
@end

@implementation PullCollectionView

# pragma mark - Initialization / Deallocation
@synthesize pullDelegate;

-(id)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout{
    self = [super initWithFrame:frame collectionViewLayout:layout];
    if (self) {
        [self config];
    }
    return self;
}

- (void)awakeFromNib
{
    [super awakeFromNib];
    [self config];
}

- (void)addFooter
{
    MJRefreshFooterView *footer = [MJRefreshFooterView footer];
    footer.scrollView = self;
    footer.isend = NO;
    self.refreshFooterView = footer;
    footer.beginRefreshingBlock = ^(MJRefreshBaseView *refreshView) {
        [self.pullDelegate pullCollectionViewDidTriggerLoadMore:self];
    };
    
}

- (void)addHeader
{
    MJRefreshHeaderView *header = [MJRefreshHeaderView header];
    header.scrollView = self;
    self.refreshHeaderView = header;
    header.beginRefreshingBlock = ^(MJRefreshBaseView *refreshView) {
        [self.pullDelegate pullCollectionViewDidTriggerRefresh:self];
    };
//    [NSTimer scheduledTimerWithTimeInterval:3.0f target:self selector:@selector(hideRefresh) userInfo:nil repeats:NO];
}
-(void)hideRefresh{
    [self.refreshHeaderView endRefreshing];
}
- (void)doneWithView:(id)sender
{
    // 刷新表格
    [self reloadData];
    [self.refreshFooterView endRefreshing];
    [self.refreshHeaderView endRefreshing];
}

- (void)dealloc
{
    [self.refreshHeaderView free];
    [self.refreshFooterView free];
    [super dealloc];
}

# pragma mark - Custom view configuration
-(void)configWithRefresh:(BOOL)isRefresh AndWithLoadMore:(BOOL)isLoadMore{
    
}

- (void)beginRefresh
{
    [self.refreshHeaderView beginRefreshing];
}

- (void)config
{
    self.alwaysBounceVertical = YES;
    [self addHeader];
    [self addFooter];
}

#pragma mark - Status Propreties
- (void)endRefreshing
{
    [self performSelector:@selector(doneWithView:) withObject:nil afterDelay:0.4];
}

- (void)endLoadingMore
{
    [self performSelector:@selector(doneWithView:) withObject:nil afterDelay:0.1];
}

@end